<?php
require_once(dirname(__FILE__) ."/Core.php");

class PrivateMessageRecipients extends Core
{
    public $_dbUserTable     = "users";
    public $_dbUsernameField = "nickname";
    public $_dbUserIDField   = "userID";

    // Recipients arrays
    public $_validated;
    public $_arRecipients;
    public $_arRecipientIDs;
    public $_arValidRecipients;
    public $_arInvalidRecipients;

    public function __construct()
    {
        $this->clear();
    }

    public function addNames($rcpts, $separator=";")
    {
        $arRcpts = explode($separator, $rcpts);
        foreach($arRcpts AS $recipient)
        {
            $recipient = $this->txt2sql($recipient);
            array_push($this->_arRecipients, $recipient);
        }
    }

    public function addIDs($rcpts, $separator=";")
    {
        $arRcpts = explode($separator, $rcpts);
        foreach($arRcpts AS $recipient)
        {
            $recipient = (int)$recipient;
            array_push($this->_arRecipientIDs, $recipient);
        }
    }

    public function validate($refresh=false)
    {
        $this->_debugLog(2, get_class($this) ."->validate()");

        if (count($this->_arRecipients)==0)
    	    return $this->_error("Please, add a valid recipient.");

        // Aleardy validated the recipients?
        if ($this->_validated && $refresh==false)
        {
            return count($this->_arValidRecipients)>0? 1 : 0;
        }

        // Validating recipients names
        if (count($this->_arRecipients)>0)
        {
            $sql = "SELECT ". $this->_dbUserIDField .", ". $this->_dbUsernameField ." FROM ". $GLOBALS["CONF"]["db"]["prefix"] . $this->_dbUserTable ."
                    WHERE ". $this->_dbUsernameField ." IN ('". implode("', '", $this->_arRecipients) ."')";
            if ($this->_mysqlQuery($sql, $result) && mysql_num_rows($result)>0)
            {
                while(list($userID, $username) = mysql_fetch_row($result))
                    $this->_arValidRecipients[$userID] = $username;
            }
        }
        // Validating recipients IDs
        if (count($this->_arRecipientIDs)>0)
        {
            $sql = "SELECT ". $this->_dbUserIDField .", ". $this->_dbUsernameField ." FROM ". $GLOBALS["CONF"]["db"]["prefix"] . $this->_dbUserTable ."
                    WHERE ". $this->_dbUserIDField ." IN ('". implode("', '", $this->_arRecipientIDs) ."')";
            if ($this->_mysqlQuery($sql, $result) && mysql_num_rows($result)>0)
            {
                while(list($userID, $username) = mysql_fetch_row($result))
                    $this->_arValidRecipients[$userID] = $username;
            }
        }
        
        // Merging invalid recipients
        $this->_arInvalidRecipients = array_merge(array_diff($this->_arRecipients, $this->_arValidRecipients),
                                                  array_diff($this->_arRecipientIDs, $this->_arValidRecipients));
        
         // returning values
        if (count($this->_arValidRecipients)==0)
        {
            $this->_arInvalidRecipients = array_merge($this->_arRecipients, $this->_arRecipientIDs);
            $this->_errorMsg = "Please, add a valid recipient.";
            return 0;
        }
        return 1;
    }

    public function getValids()
    {
        return $this->_arValidRecipients;
    }

    public function getInvalids()
    {
        return $this->_arInvalidRecipients;
    }

    public function clear()
    {
        $this->_arRecipients = array();
        $this->_arRecipientIDs = array();
        $this->_arValidRecipients = array();
        $this->_arInvalidRecipients = array();
        $this->_validated = false;
    }
}

?>
